package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Dish;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_CHEF_DUEL2_MERGE_DISH, desc = "挑完菜合菜合菜")
public class ChefDuel2MergeDishCmd extends AbstractCommand {

	int activityId;

	List<ChefDuel2Dish> cookBoy2DishList;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2MergeDishReqMsg reqMsg = ChefDuelProto.ChefDuel2MergeDishReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getInTimeActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_RELATION_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(!ChefDuelActivityMgr.isActivityMatchTime(activityInfo)){
			ret = GameErrorCode.E_CHEF_DUEL_NOT_IN_MATCH_TIME;
			return;
		}
		if(!player.getModule(ChefDuelModule.class).isAble(activityId)){
			ret = GameErrorCode.E_CHEF_DUEL2_USER_NOT_ABLE;
			return;
		}
		if(!player.getModule(ChefDuelModule.class).hasCamp(activityId)){
			ret = GameErrorCode.E_CHEF_DUEL2_CAMP_NO_SELECT;
			return;
		}
		cookBoy2DishList = ChefDuelMgr.dishMerge(activityId,player.getUserId());
	}

	@Override
	public void sendData(GamePlayer player) {
		ChefDuelProto.ChefDuel2MergeDishRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2MergeDishRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(cookBoy2DishList != null){
			for (ChefDuel2Dish cookBoy2Dish : cookBoy2DishList) {
				ChefDuelProto.ChefDuel2DishTempMsg.Builder finishTemp = ChefDuelProto.ChefDuel2DishTempMsg.newBuilder();
				finishTemp.setId(cookBoy2Dish.getIndex());
				finishTemp.setDishId(cookBoy2Dish.getDishId());
				finishTemp.setStatus(cookBoy2Dish.getStatus());
				finishTemp.setIntegral(cookBoy2Dish.getIntegral());
				finishTemp.setTableId(cookBoy2Dish.getTableId());
				finishTemp.setMakeIntegral(cookBoy2Dish.getMakeIntegral());
				finishTemp.setMakeAddition(cookBoy2Dish.getMakeAddition());
				respMsg.addDish(finishTemp);
			}
		}else {
			if(ret == 0) {
				respMsg.setRet(GameErrorCode.E_CHEF_DUEL2_CAMP_PICK_TIME_NO_ENOUGH);
			}
		}
		player.sendPacket(Protocol.U_CHEF_DUEL2_MERGE_DISH, respMsg);
	}
}
